/*
 ============================================================================
 Name        : PCB.java
 Author      : Dallas Rosson, Joshua Warner, Tyson Lewis, Ken Norton
 Team        : Bitwise Guys
 ============================================================================
 */
package backend;

/**
 * PCB
 * 
 * A PCB construct.
 * 
 * @author Dallas Rosson, Joshua Warner, Tyson Lewis, Ken Norton
 * @version 1.0, 5/26/2012
 */
public class PCB {
	private int	pid;
	private int next_step;		// this is the step count that the CPU had gotten to when this process was
								// prempted (like a PC register value)
	private int state;			// e.g. 0=running, 1=ready, 2=interrupted, 3=blocked
	private SchedulerProcess proc;    // pointer to the actual process
	private int waiting_on;		// which queue is it in if it is waiting on something (blocked)
	private int owns;			// which mutex lock does it own
	
	public PCB(int the_pid, SchedulerProcess the_proc){
		pid = the_pid;
		next_step = 0;
		state = 1;
		proc = the_proc;
		waiting_on = 0;
		owns = 0;
	}
	
	/**
	 * 
	 * @return the unique id of the process associated with this PCB.
	 */
	public int getPid(){
		return pid;
	}
	
	/**
	 * 
	 * @return the index of the next instruction to be executed.
	 */
	public int getNextStep(){
		return next_step;
	}
	
	/**
	 * state of the machine is one of four types
	 * 0=running, 1=ready, 2=interrupted, 3=blocked
	 * @return the current state of the associated process.
	 */
	public int getState(){
		return state;
	}
	
	/**
	 * 
	 * @return the process associated with this PCB.
	 */
	public SchedulerProcess getProc(){
		return proc;
	}
	
	/**
	 * 
	 * @return the queue number the associated process is waiting in.
	 */
	public int getWaitingOn(){
		return waiting_on;
	}
	
	/**
	 * a return of 0 means that the associated process owns no lock.
	 * @return the lock number that the associated process owns if any.
	 */
	public int getLockOwned(){
		return owns;
	}

	/**
	 * Set the next step that this process is on.
	 * @param next_step the next step.
	 */
	public void setNextStep(int next_step) {
		this.next_step = next_step;
	}

	/**
	 * Set the current State of this process.
	 * @param state the current state.
	 */
	public void setState(int state) {
		this.state = state;
	}

	/**
	 * set the Queue number that this process is waiting on
	 * @param waiting_on the Queue number.
	 */
	public void setWaiting_on(int waiting_on) {
		this.waiting_on = waiting_on;
	}

	/**
	 * Set the Lock that this Process holds.
	 * @param owns the lock number owned.
	 */
	public void setOwns(int owns) {
		this.owns = owns;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object obj){
		boolean are_equal = true;
		
		if(obj.getClass() == PCB.class){
			PCB other = (PCB)obj;
			if(pid != other.pid){
				are_equal = false;
			}
		}else{
			are_equal = false;
		}
		return are_equal;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public int hashCode(){
		return Integer.toString(pid).hashCode();
	}
	
	
	
	
	
}
